﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class SkillListManager : LTEventListenerC {
	public List<SkillListItem> itemList = new List<SkillListItem>();
	public DeckEditSceneManager sceneManager;
	
	// Use this for initialization
	void Start () {
        RestAPI.singleton.GetMySkillList(RestCallback);
        /*
		int index = 0;
        //DeckInfo deckInfo = MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck];
		foreach(CSkillData skillData in CSkillDataManager.Instance.GetSkillList())
		{
			if( skillData.category == 2 )
			{	
				SkillListItem item = LTPrefabManager.InstantiateC(PrefabTypes.Panel_SkillBar1, Vector3.one, Quaternion.identity) as SkillListItem;
				Transform itemTrans = item.transform;
				itemTrans.parent = transform;
                itemTrans.localScale = Vector3.one;
				item.skillIcon.spriteName = skillData.icon_name;
				item.skillNameLabel.text = CStringTableManager.Instance.GetItemByName(skillData.name_string);
				item.skillDescLabel.text = CStringTableManager.Instance.GetItemByName(skillData.desc_string);
				item.skillData = skillData;
				item.requireMPLabel.text = "MP: " + skillData.cost.ToString();
				LTButtonMessage button = item.GetComponent<LTButtonMessage>();
				button.m_MethodDeliver[0].m_EventListener = this;
				button.m_MethodDeliver[0].m_EventId = index;
				item.index = index++;
				itemList.Add(item);
			}
		}
		
		RefreshItemStatus();
		
		GetComponent<UIGrid>().Reposition();
        */
	}

    void OnEnable()
    {
        RefreshItemStatus();
    }
	
	// Update is called once per frame
	void Update () {
		
	}
	
	public void RefreshItemStatus()
	{
		DeckInfo deckInfo = MyInfo.Instance.MyDeckMap[MyInfo.Instance.MyCurrentDeck];
		foreach(SkillListItem item in itemList)
		{
			if( item.skillData.id != deckInfo.skill1 && item.skillData.id != deckInfo.skill2 )
			{
                //item.selectButton.SetActive(true);
                //item.background.spriteName = "UI_btn03s";
				item.selectedMark.gameObject.SetActive(false);
                item.collider.enabled = true;
 	 		}
			else if( sceneManager.selectedSkillIconIndex == 0 )
			{
				if( item.skillData.id == deckInfo.skill1 )
				{
                    //item.selectButton.SetActive(false);
                    //item.background.spriteName = "UI_btn02s";
					item.selectedMark.gameObject.SetActive(true);
                    item.collider.enabled = false;
				}
				else if( item.skillData.id == deckInfo.skill2 )
				{
                    //item.selectButton.SetActive(false);
                    //item.background.spriteName = "UI_btn03sGray";
					item.selectedMark.gameObject.SetActive(true);
                    item.collider.enabled = false;
				}
			}
			else if( sceneManager.selectedSkillIconIndex == 1 )
			{
				if( item.skillData.id == deckInfo.skill1 )
				{
                    //item.background.spriteName = "UI_btn03sGray";
					item.selectedMark.gameObject.SetActive(true);
                    item.collider.enabled = false;
				}
				else if( item.skillData.id == deckInfo.skill2 )
				{
                    //item.selectButton.SetActive(false);
                    //item.background.spriteName = "UI_btn02s";
					item.selectedMark.gameObject.SetActive(true);
                    item.collider.enabled = false; 
				}
			}
		}
	}
	
	public int currentSelectedIndex = -1;
	public override void OnEvent (int eventID)
	{
		base.OnEvent (eventID);
		
		currentSelectedIndex = eventID;
		
//		itemList[currentSelectedIndex].selectButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01";
		
		sceneManager.SetSkillIcon(itemList[eventID].skillData.id, itemList[eventID].skillData.icon_name);
		
		transform.parent.parent.gameObject.SetActive(false);
	}

	public void RestCallback(Hashtable returnData)
	{
		switch((RestAPIProtocol)returnData["protocol"])
		{
		case RestAPIProtocol.GETMYSKILLLIST:
			int index = 0;
			foreach(int skillId in MyInfo.Instance.MySkillList)
			{
				Debug.LogError(skillId);
				CSkillData skillData = CSkillDataManager.Instance.FindSkillData(skillId);
				if( skillData.category == 2 )
				{	
					Debug.LogError(skillData.name);
					SkillListItem item = LTPrefabManager.InstantiateC(PrefabTypes.Panel_SkillBar1, Vector3.one, Quaternion.identity) as SkillListItem;
					Transform itemTrans = item.transform;
					itemTrans.parent = transform;
                    itemTrans.localScale = Vector3.one;
					item.skillIcon.spriteName = skillData.icon_name;
					item.skillNameLabel.text = CStringTableManager.Instance.GetItemByName(skillData.name_string);
					item.skillDescLabel.text = CStringTableManager.Instance.GetItemByName(skillData.desc_string);
					item.skillData = skillData;
					item.requireMPLabel.text = "MP: " + skillData.cost.ToString();
                    LTButtonMessage button = item.GetComponent<LTButtonMessage>();
					button.m_MethodDeliver[0].m_EventListener = this;
					button.m_MethodDeliver[0].m_EventId = index;
					item.index = index++;
					itemList.Add(item);
				}
			}

			RefreshItemStatus();
			
			GetComponent<UIGrid>().Reposition();
			break;
		}
	}
}
